package com.google.code.mochaccino.framework.ui.demo.pages;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.freemarker.TemplateManager;
import com.google.code.mochaccino.framework.rest.mime.MimeTypes;
import com.google.code.mochaccino.framework.ui.demo.SubjectUtil;
import com.google.code.mochaccino.framework.ui.freemarker.TemplateOutput;
import com.google.code.mochaccino.framework.ui.i18n.ResourceBundleUtil;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.inject.Inject;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.shiro.authz.annotation.RequiresGuest;


/**
 *
 */
@Path( ForgotPassword.PATH )
public class ForgotPassword {


	private LoginService service;

	private TemplateManager manager;

	public static final String PATH_SUCCESS = "/success";
	public static final String PATH         = "/forgot_pwd";


	@Inject
	public ForgotPassword( TemplateManager manager, LoginService service ) {
		this.manager = manager;
		this.service = service;
	}

	/** The Style */
	@Path( PATH_SUCCESS )
	@GET
	@Produces( MimeTypes.TYPE_CSS )
	@RequiresGuest

	public Response forgotPasswordSuccess_css() {
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/forgot_password/forgot_password.less.ftl" ) ).build();
	}

	/** Login HTML */
	@Path( PATH_SUCCESS )
	@GET
	@Produces( MediaType.TEXT_HTML )
	@RequiresGuest

	public Response forgotPasswordSuccess_html( @QueryParam( "lang" ) String lang ) {
		Locale loc = SubjectUtil.setLocale( lang );
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put( "env", service.getEnvironment() );
		parameters.put( "i18n", ResourceBundleUtil.getResourceBundleModel( "web/pages/forgot_password_success/forgot_password_success", loc ) );
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/forgot_password_success/forgot_password_success.html.ftl", parameters ) ).build();
	}

	/** The Style */
	@GET
	@Produces( MimeTypes.TYPE_CSS )
	@RequiresGuest

	public Response forgotPassword_css() {
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/forgot_password/forgot_password.less.ftl" ) ).build();
	}

	/** Login HTML */
	@GET
	@Produces( MediaType.TEXT_HTML )
	@RequiresGuest

	public Response forgotPassword_html( @QueryParam( "lang" ) String lang ) {
		Locale loc = SubjectUtil.setLocale( lang );
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put( "env", service.getEnvironment() );
		parameters.put( "i18n", ResourceBundleUtil.getResourceBundleModel( "web/pages/forgot_password/forgot_password", loc ) );
		return Response.ok().entity( new TemplateOutput( manager, "/web/pages/forgot_password/forgot_password.html.ftl", parameters ) ).build();
	}

	/** Reset */
	@POST
	@Produces( MediaType.TEXT_HTML )
	@RequiresGuest

	public Response forgotPassword_reset( @FormParam( "username" ) String username ) {
		if ( username == null || username.length() == 0 ) {
			return Response.status( Status.BAD_REQUEST ).entity( "Form parameter 'username' cannot be null" ).build();
		}

		service.reset( username );
		return Response.ok().entity( PATH + PATH_SUCCESS ).build();
	}
}
